$(document).ready(function() {
	
	$('#DB_CLASS_LIST li').each(function(index) {
		class_id = $(this).attr('flexyclass_id');
		str = ' \
		<tr> \
			<td colspan="2"> \
				<table class="flexyclass-table" border="0" cellspacing="0"> \
			<tr> \
					<tr> \
						<td></td> \
						<td class="permoptions-td"> Read Write </td> \
						<td class="permoptions-td"> Read Only </td> \
						<td class="permoptions-td"> Hide </td> \
					</tr> \
					<tr id="CLASS_' + class_id + '"> \
						<td class="flexyclassname-td"> \
							<a name="' + class_id +
							'" href="/sysmodel/flexyclass/update/' + class_id 
							+ '/"><h4> ' + $(this).html() + '</h4></a> \
						</td> \
						<td class="flexyclassoptions-td"> \
							<input entity="CLASS" type="radio" class_id="' +  class_id + '" name="CLS_' + class_id + '" value="2" /> \
						</td> \
						<td class="flexyclassoptions-td"> \
							<input entity="CLASS" type="radio" class_id="' +  class_id + '" name="CLS_' + class_id + '" value="1" />  \
						</td> \
						<td class="flexyclassoptions-td"> \
							<input entity="CLASS" type="radio" class_id="' +  class_id + '" name="CLS_' + class_id + '" value="0" /> \
						</td> \
					</tr> \
				</table> \
			</td> \
		</tr>';
		$('#UI_CLASS_LIST').append(str);
	});
	
	
	$('#DB_GROUP_LIST li').each(function(index) {
		group_id = $(this).attr('group_id');
		str = ' \
		<tr id="GROUP_' + group_id + '"> \
			<td class="groupname-td"> \
				<a href="/sysmodel/flexyclass/group/update/' + 
					group_id + '/"><h5>' + $(this).html() + '</h5></a> \
			</td> \
			<td class="groupoptions-td"></td> \
			<td class="groupoptions-td"></td> \
			<td class="groupoptions-td"></td> \
		</tr>';
		$(str).insertAfter('#CLASS_' + $(this).attr('flexyclass_id'));
	});
	
	
	$('#DB_FIELD_LIST li').each(function(index) {
		
		field_id = $(this).attr('field_id');
		field_type = $(this).attr('fieldtype');
		class_id = $(this).attr('class_id');
		
		str = ' \
			<tr id="FIELD_' + field_id + '"> \
				<td class="fieldname-td"> \
					<a href="/sysmodel/flexyclass/field/update/' + 
						field_id + '/" title="' + field_type + '">'
						+ $(this).html() + '</a> \
				</td> \
				<td class="fieldoptions-td"> \
					<input entity="FIELD" type="radio" class_id="' +  class_id + '" name="FLD_'+ field_id + '" value="2" /> \
				</td> \
				<td class="fieldoptions-td"> \
					<input entity="FIELD" type="radio" class_id="' +  class_id + '" name="FLD_'+ field_id + '" value="1" />  \
				</td> \
				<td class="fieldoptions-td"> \
					<input entity="FIELD" type="radio" class_id="' +  class_id + '" name="FLD_'+ field_id + '" value="0" /> \
				</td> \
			</tr>';
		$(str).insertAfter('#GROUP_' + $(this).attr('group_id'));
	});
	
	$('#DB_DISPLAY_FIELD_LIST li').each(function(index) {
		parent_field_id = $(this).attr('parent_field_id');
		field_id =  $(this).attr('self_field_id');
		str = ' \
			<tr id="DISPLAY_FIELD_' + field_id + '"> \
				<td class="displayfieldname-td"><span>[' + $(this).html() + ']</span></td> \
				<td class="displayfieldoptions-td"></td> \
				<td class="displayfieldoptions-td"></td> \
				<td class="displayfieldoptions-td"></td> \
			</tr>';
		$(str).insertAfter('#FIELD_' + parent_field_id);
	});
	
	$('#DB_EXTRA_FIELD_LIST li').each(function(index) {
		field_id = $(this).attr('self_field_id');
		class_id = $(this).attr('class_id');
		
		str = ' \
			<tr id="EXTRA_FIELD_' + field_id + '"> \
				<td class="extrafieldname-td"><a href="/sysmodel/flexyclass/field/extrafield/update/' 
				 + field_id + '/" title="' + $(this).attr('fieldtype') + '">' + $(this).html() +  '</a></td> \
				<td class="extrafieldoptions-td"> \
					<input entity="FIELD" type="radio" class_id="' +  class_id + '" name="FLD_' + field_id + '" value="2" /> \
				</td> \
				<td class="extrafieldoptions-td"> \
					<input entity="FIELD" type="radio" class_id="' +  class_id + '" name="FLD_' + field_id + '" value="1" />  \
				</td> \
				<td class="extrafieldoptions-td"> \
					<input entity="FIELD" type="radio" class_id="' +  class_id + '" name="FLD_' + field_id + '" value="0" /> \
				</td> \
			</tr>';
		$(str).insertAfter('#FIELD_' + $(this).attr('parent_field_id'));
	});
	
	str = '<tr> \
			<td class="list-heading"> \
				<input class="submit" type="submit" value="Save"/> \
			</td> \
			<td class="list-tools"></td> \
		</tr>';
	$('#UI_CLASS_LIST').append(str);
	
	$('input[entity="CLASS"]').click(function() {
		class_id = $(this).attr('class_id');
		perm = $(this).val();
		$('input[class_id=' + class_id + ']').each(function(index) {
			if ($(this).attr('entity') == 'FIELD') 
			{
				if ($(this).val() == perm) {
					$(this).attr('checked', 'yes');
				}
				if ($(this).val() > perm) {
					$(this).attr('disabled', 'yes');
				}
				if ($(this).val() <= perm) {
					$(this).removeAttr('disabled');
				}
			}
		});
	});

	$('#CLASS_PERM_LIST li').each(function(index) {
		radio_button_id = $(this).attr('class_id');
		perm = $(this).html();
		
		$('input[name=CLS_' + radio_button_id + ']').each(function(index) {
			if ($(this).val() == parseInt(perm)) {
				//$(this).attr('checked','yes');
				$(this).trigger('click');
			}
		});
	});

	$('#FIELD_PERM_LIST li').each(function(index) {
		radio_button_id = $(this).attr('field_id');
		perm = $(this).html();
		
		$('input[name=FLD_' + radio_button_id + ']').each(function(index) {
			if (parseInt($(this).val()) == parseInt(perm)) {
				$(this).attr('checked','yes');
			}
		});
	});
	

}); //document ready
